clear
set more off
*version 8
capture log close

use "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 6 Regionalism\Data\base panel mexico 2006.dta", clear 

gen respond1=1 if dia~=.
gen respond2=1 if dia_2~=.
gen respond3=1 if dia_3~=.
recode respond* (.=0)

*I am downweighting the states that were oversampled, including df
gen pweight=1
replace pweight=160/	(160+500) if estado==9
replace pweight=80/(80+140) if estado==7
replace pweight=100/(80+100) if estado==14
replace pweight=60/(80+60) if estado==20	
svyset [pweight=pweight], psu(municipi)

*note that those in the cross-sectional samples answered shorter questionnaires and were not reinterviewed
drop if tipomu_2==4
drop if tipomu_3==4

*----------------------------------

*DVS
	*CHANGED VOTE (DV)
label define vote 1 "Felipe Calderon" 2 "Roberto Madrazo" 3 "AMLO" 5 "Patricia Mercado" 6 "Others" 7 "Abstain"
recode p8 4=. 6=7 7/9=7 .=7, gen(vote1)
replace vote1=. if respond1==0
label values vote1 vote

recode p7_2 4=6 6=6 7/9=7 .=7, gen(vote2)
replace vote2=. if respond2==0
label values vote2 vote

recode p6_3 4=6 6=6 7/9=7 .=7, gen(vote3)
replace vote3=. if respond3==0
label values vote3 vote

		*Of voters with candidate preferences (excludes DK from denominator, and also none/abstention and nr)
			*in both waves (1 and 2; 2 and 3)
gen change12=0 if respond1==1 & respond2==1 & p8~=4 & p8~=6 & p8~=7 & p8~=8 & p8~=. & p7_2~=7 & p7_2~=9 & p7_2~=4 & p7_2~=6 & p7_2~=8 & p7_2~=.
replace change12=1 if p8~=p7_2 & p8<6 & p7_2<7 & change12==0
bysort change12: tab p8  p7_2, m
svy: tab change12


gen change23=0 if respond2==1 & respond3==1 & p7_2~=7 & p7_2~=9 & p7_2~=. & p6_3~=7 & p6_3~=9 & p7_2~=8 & p6_3~=8 & p6_3~=.
replace change23=1 if p7_2~=p6_3 & p7_2<7 & p6_3<7 & change23==0
bysort change23: tab p7_2 p6_3, m
svy: tab change23

gen change13=0 if respond1==1 & respond3==1 & p8~=4 & p8~=6 & p8~=7 & p8~=8 & p8~=.  & p6_3~=7 & p6_3~=9 & p6_3~=8 & p6_3~=.
replace change13=1 if p8~=p6_3 & p8<6 & p6_3<7 & change13==0
bysort change13: tab p8 p6_3, m
svy: tab change13

			*in all three waves (1-3)
gen changedatall=change23+change12
recode changedatall 2=1
svy: tab changedatall

		*Strategic voting stuff for chapter 3
gen major_party_not_strategic13=1 if (p8==1 & p6_3==3) | (p8==3 & p6_3==1)
replace major_party_not_strategic13=0 if change13~=. & major_party_not_strategic13==.
gen strategic13=1 if (p8==2 & p6_3==1) |  (p8==2 & p6_3==3) | (p8==5 & p6_3==3) |  (p8==5 & p6_3==1) | (p8==4 & p6_3==3) |  (p8==4 & p6_3==1) | (p8==6 & p6_3==3) |  (p8==6 & p6_3==1)
replace strategic13=0 if change13~=. & strategic13==.
gen reverse_strategic13=1 if (p8==1 & p6_3==2) |  (p8==3 & p6_3==2) | (p8==3 & p6_3==5) |  (p8==1 & p6_3==5) | (p8==3 & p6_3==4) |  (p8==1 & p6_3==4) | (p8==3 & p6_3==6) |  (p8==1 & p6_3==6)
replace reverse_strategic13=0 if change13~=. & reverse_strategic13==.

	*SE cell is percent of switchers switching between top two, couldn't be strategic
svy: tab change13 major_party_not_strategic13, row
	*SE cell is percent of switchers possibly strategic
svy: tab change13 strategic13, row cel
	*SE cell is percent of switchers switching to bottom, couldn't be strategic
svy: tab change13 reverse_strategic13, row

gen major_party_not_strategic23=1 if (p7_2==1 & p6_3==3) | (p7_2==3 & p6_3==1)
replace major_party_not_strategic23=0 if change23~=. & major_party_not_strategic23==.
gen strategic23=1 if (p7_2==2 & p6_3==1) |  (p7_2==2 & p6_3==3) | (p7_2==5 & p6_3==3) |  (p7_2==5 & p6_3==1) | (p7_2==6 & p6_3==3) |  (p7_2==6 & p6_3==1) | (p7_2==4 & p6_3==3) |  (p7_2==4 & p6_3==1) 
replace strategic23=0 if change23~=. & strategic23==.
gen reverse_strategic23=1 if (p7_2==1 & p6_3==2) |  (p7_2==3 & p6_3==2) | (p7_2==3 & p6_3==5) |  (p7_2==1 & p6_3==5) | (p7_2==3 & p6_3==6) |  (p7_2==1 & p6_3==6) | (p7_2==3 & p6_3==4) |  (p7_2==1 & p6_3==4) 
replace reverse_strategic23=0 if change23~=. & reverse_strategic23==.
	*SE cell is percent of switchers switching between top two, couldn't be strategic
svy: tab change23 major_party_not_strategic23, row
	*SE cell is percent of switchers possibly strategic
svy: tab change23 strategic23, row cel
	*SE cell is percent of switchers switching to bottom, couldn't be strategic
svy: tab change23 reverse_strategic23, row


*NOW WITH INDECISOS
gen switch12=0 if respond1==1 & respond2==1 & p8~=4 & p7_2~=7 & p7_2~=9 & p7_2~=4 & p7_2~=6 & p7_2~=8 & p7_2~=.
replace switch12=1 if p8~=p7_2 & p7_2<7 & switch12==0
replace switch12=2 if (p8==7 & switch12~=.) |  (p8==8 & switch12~=.) |(p8==6 & switch12~=.) 
bysort switch12: tab p8  p7_2, m
svy: tab switch12

gen switch13=0 if respond1==1 & respond3==1 & p8~=4 & p6_3~=4 & p6_3~=7 & p6_3~=9 & p6_3~=8 & p6_3~=. & p6_3~=6  
replace switch13=1 if p8~=p6_3 & p6_3<7 & switch13==0
replace switch13=2 if (p8==7 & switch13~=.) |  (p8==8 & switch13~=.) |(p8==6 & switch13~=.) 
bysort switch13: tab p8 p6_3, m
svy: tab switch13

gen switch23=0 if respond2==1 & respond3==1 &  p6_3~=7 & p6_3~=9 & p6_3~=8 & p6_3~=.
replace switch23=1 if p6_3~=p7_2 & p6_3<7 & switch23==0
replace switch23=2 if (p7_2==7 & switch23~=.) |  (p7_2==8 & switch23~=.) |(p7_2==9 & switch23~=.)
bysort switch23: tab p7_2 p6_3, m
svy: tab switch23

			*in all three waves (1-3)
recode switch12-switch23 (2=1)
egen switchedatall=rsum(switch12 switch23)
replace switchedatall=. if respond1==0 | respond2==0 | respond3==0
replace switchedatall=. if p6_3>6
replace switchedatall=. if p8==4
tab switchedatall
recode switchedatall 2=1

*IVs
	*DISCUSSANT IVS
		*Count discussants
egen disctotal2=rsum( p66a_2 p66b_2 p66c_2)
replace disctotal2=. if respond2~=1

egen disctotal3=rsum(p48a_3 p48b_3 p48c_3)
replace disctotal3=. if respond3~=1

gen disctotal2sq=disctotal2^2
gen disctotal3sq=disctotal3^2

egen disctotalmean=rmean(disctotal2 disctotal3)
gen disctotalmeansq=disctotalmean^2

egen conversemean=rmean(p3 p3_2)
gen conversemeansq=conversemean^2

*reported in chapter 3: can't do in a single command b/c would limit sample
polychoric disctotal2 disctotal3
polychoric disctotal2 p3_2 
polychoric disctotal3 p47_3
polychoric p3 p3_2
polychoric p3 p47_3
polychoric p3_2 p47_3

************
***WAVE 2: DEFINING DISAGREEMENT AND HETEROGENEITY
egen numdisc2=rsum( p66a_2 p66b_2 p66c_2)
replace numdisc2=. if respond2~=1
replace p68a_2=. if p66a_2~=1
replace p68b_2=. if p66b_2~=1
replace p68c_2=. if p66c_2~=1

recode p68a_2-p68c_2 (1=1) (6=.) (7=.) (else=0), gen(felipe21 felipe22 felipe23)
recode p68a_2-p68c_2 (2=1) (6=.) (7=.) (else=0), gen(madrazo21 madrazo22 madrazo23)
recode p68a_2-p68c_2 (3=1) (6=.) (7=.) (else=0), gen(amlo21 amlo22 amlo23)
recode p68a_2-p68c_2 (4=1) (6=.) (7=.) (else=0), gen(campa21 campa22 campa23)
recode p68a_2-p68c_2 (5=1) (6=.) (7=.) (else=0), gen(mercado21 mercado22 mercado23)
recode p68a_2-p68c_2 (6=1) (7=1) (else=0), gen(noop21 noop22 noop23)
egen totopinionateddisc2=rsum(felipe21 felipe22 felipe23 madrazo21 madrazo22 madrazo23 amlo21 amlo22 amlo23 campa21 campa22 campa23 mercado21 mercado22 mercado23)
replace totopinionateddisc2=. if respond2~=1

gen agree21=0
replace agree21=1 if p68a_2==p7_2
replace agree21=. if p7_2>6
replace agree21=. if p68a_2>5
gen agree22=0
replace agree22=1 if p68b_2==p7_2
replace agree22=. if p7_2>6
replace agree22=. if p68b_2>5
gen agree23=0
replace agree23=1 if p68c_2==p7_2
replace agree23=. if p7_2>6
replace agree23=. if p68c_2>5
replace agree21=. if respond2~=1
replace agree22=. if respond2~=1
replace agree23=. if respond2~=1
egen numagree2=rsum(agree21-agree23)
replace numagree2=. if respond2==0
replace numagree2=. if p7_2>6

	*STRONG
		*DISAGREEMENT is presence of disagreement over candidates (Sokhey et al AJPS)
				*Note that it only looks at discussants with reported opinions
gen disagreementstrong2=1-numagree2/totopinionateddisc2
replace disagreementstrong2=0 if totopinionateddisc2==0
replace disagreementstrong2=. if p7_2>5

		*HETEROGENEITY 
recode agree21 0=1 1=0, gen(disagree21)
recode agree22 0=1 1=0, gen(disagree22)
recode agree23 0=1 1=0, gen(disagree23)
egen numdisagree2=rowtotal(disagree21 disagree22 disagree23)
forvalues i=1(1)3 {
replace felipe2`i'=. if agree2`i'==1
replace madrazo2`i'=. if agree2`i'==1
replace amlo2`i'=. if agree2`i'==1
replace campa2`i'=. if agree2`i'==1
replace mercado2`i'=. if agree2`i'==1
}

egen felipesum_numerator2=rsum(felipe21 felipe22 felipe23)
egen madrazosum_numerator2=rsum(madrazo21 madrazo22 madrazo23)
egen amlosum_numerator2=rsum(amlo21 amlo22 amlo23)
egen campasum_numerator2=rsum(campa21 campa22 campa23)
egen mercadosum_numerator2=rsum(mercado21 mercado22 mercado23)
gen felipediscprop2=felipesum_numerator2 / numdisagree2
gen madrazodiscprop2= madrazosum_numerator2 /numdisagree2
gen amlodiscprop2=amlosum_numerator2 /numdisagree2
gen campadiscprop2= campasum_numerator2/numdisagree2
gen mercadodiscprop2=mercadosum_numerator2/numdisagree2
gen heterogeneitystrong2 = 1-(felipediscprop2^2+madrazodiscprop2^2+amlodiscprop2^2+campadiscprop2^2+mercadodiscprop2^2)
recode heterogeneitystrong2 .=0
replace heterogeneitystrong2 =. if numdisagree2<2 & numdisagree2~=. & disagreementstrong2==. 
replace heterogeneitystrong2 =. if respond2==0
replace heterogeneitystrong2 =. if p7_2>5
drop disagree21 disagree22 disagree23  numdisagree2

		*SINGLETON
recode totopinionateddisc2 0=1 .=. else=0, gen(zerodiscstrong2)
replace zerodiscstrong2=. if respond2==0

	*WEAK
		*DISAGREEMENT is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak2=1-numagree2/numdisc2
replace disagreementweak2=0 if numdisc2==0
replace disagreementweak2=. if p7_2>5

		*HETEROGENEITY 
replace agree21=0 if p68a_2>5 & p66a_2==1 & p7_2<6
replace agree22=0 if p68b_2>5 & p66b_2==1 & p7_2<6
replace agree23=0 if p68c_2>5 & p66c_2==1 & p7_2<6

recode agree21 0=1 1=0, gen(disagree21)
recode agree22 0=1 1=0, gen(disagree22)
recode agree23 0=1 1=0, gen(disagree23)

egen numdisagree2=rowtotal(disagree21 disagree22 disagree23)
forvalues i=1(1)3 {
replace noop2`i'=. if disagree2`i'==0  
}

egen noopsum_numerator2=rsum(noop21 noop22 noop23)
gen felipediscprop2weak=felipesum_numerator2 / numdisagree2
gen madrazodiscprop2weak= madrazosum_numerator2 /numdisagree2
gen amlodiscprop2weak=amlosum_numerator2 /numdisagree2
gen campadiscprop2weak= campasum_numerator2/numdisagree2
gen mercadodiscprop2weak=mercadosum_numerator2/numdisagree2
gen noopdiscprop2weak=noopsum_numerator2/numdisagree2
gen heterogeneityweak2 = 1-(felipediscprop2weak^2+madrazodiscprop2weak^2+amlodiscprop2weak^2+campadiscprop2weak^2+mercadodiscprop2weak^2+noopdiscprop2weak^2)
recode heterogeneityweak2 .=0
replace heterogeneityweak2 =. if numdisagree2<2 & numdisagree2~=. & disagreementweak2==. 
replace heterogeneityweak2=. if respond2==0
replace heterogeneityweak2=. if p7_2>5
recode heterogeneityweak2 1=. 

		*SINGLETONS
recode disctotal2 0=1 .=. else=0, gen(zerodiscweak2)
replace zerodiscweak2=. if respond2==0

***WAVE 3: DEFINING DISAGREEMENT AND HETEROGENEITY
egen numdisc3=rsum(  p48a_3 p48b_3 p48c_3)
replace numdisc3=. if respond3~=1
replace p50a_3=. if p48a_3~=1
replace p50b_3=. if p48b_3~=1
replace p50c_3=. if p48c_3~=1
recode p50a_3-p50c_3 (1=1) (6=.) (7=.) (else=0), gen(felipe31 felipe32 felipe33)
recode p50a_3-p50c_3 (2=1) (6=.) (7=.) (else=0), gen(madrazo31 madrazo32 madrazo33)
recode p50a_3-p50c_3 (3=1) (6=.) (7=.) (else=0), gen(amlo31 amlo32 amlo33)
recode p50a_3-p50c_3 (4=1) (6=.) (7=.) (else=0), gen(campa31 campa32 campa33)
recode p50a_3-p50c_3 (5=1) (6=.) (7=.) (else=0), gen(mercado31 mercado32 mercado33)
recode p50a_3-p50c_3 (6=1) (7=1) (else=0), gen(noop31 noop32 noop33)
egen totopinionateddisc3=rsum(felipe31 felipe32 felipe33 madrazo31 madrazo32 madrazo33 amlo31 amlo32 amlo33 campa31 campa32 campa33 mercado31 mercado32 mercado33)
replace totopinionateddisc3=. if respond3~=1

				*Note this uses wave 2 MR vote choice
gen agree31=0
replace agree31=1 if p50a_3==p7_2
replace agree31=. if p7_2>6
replace agree31=. if p50a_3>5
gen agree32=0
replace agree32=1 if p50b_3==p7_2
replace agree32=. if p7_2>6
replace agree32=. if p50b_3>5
gen agree33=0
replace agree33=1 if p50c_3==p7_2
replace agree33=. if p7_2>6
replace agree33=. if p50c_3>5
egen numagree3=rsum(agree31-agree33)
replace numagree3=. if respond3==0
replace numagree3=. if p7_2>5

	*STRONG
		*DISAGREEMENT is presence of disagreement over candidates (Sokhey et al AJPS)
				*Note that it only looks at discussants with reported opinions
gen disagreementstrong3=1-numagree3/totopinionateddisc3
replace disagreementstrong3=0 if totopinionateddisc3==0
replace disagreementstrong3=. if p7_2>5

		*HETEROGENEITY 
recode agree31 0=1 1=0, gen(disagree31)
recode agree32 0=1 1=0, gen(disagree32)
recode agree33 0=1 1=0, gen(disagree33)
egen numdisagree3=rowtotal(disagree31 disagree32 disagree33)
forvalues i=1(1)3 {
replace felipe3`i'=. if agree3`i'==1
replace madrazo3`i'=. if agree3`i'==1
replace amlo3`i'=. if agree3`i'==1
replace campa3`i'=. if agree3`i'==1
replace mercado3`i'=. if agree3`i'==1
}
egen felipesum_numerator3=rsum(felipe31 felipe32 felipe33 )
egen madrazosum_numerator3=rsum(madrazo31 madrazo32 madrazo33)
egen amlosum_numerator3=rsum(amlo31 amlo32 amlo33)
egen campasum_numerator3=rsum(campa31 campa32 campa33)
egen mercadosum_numerator3=rsum(mercado31 mercado32 mercado33)
gen felipediscprop3=felipesum_numerator3 / numdisagree3
gen madrazodiscprop3= madrazosum_numerator3 /numdisagree3
gen amlodiscprop3=amlosum_numerator3 /numdisagree3
gen campadiscprop3= campasum_numerator3/numdisagree3
gen mercadodiscprop3=mercadosum_numerator3/numdisagree3
gen heterogeneitystrong3 = 1-(felipediscprop3^2+madrazodiscprop3^2+amlodiscprop3^2+campadiscprop3^2+mercadodiscprop3^2)
recode heterogeneitystrong3 .=0
replace heterogeneitystrong3 =. if numdisagree3<2 & numdisagree3~=. & disagreementstrong3==.
replace heterogeneitystrong3 =. if respond3==0
replace heterogeneitystrong3=. if p7_2>5
drop disagree31 disagree32 disagree33  numdisagree3

		*SINGLETONS
recode totopinionateddisc3 0=1 .=. else=0, gen(zerodiscstrong3)
replace zerodiscstrong3=. if respond3==0

	*WEAK 
		*DISAGREEMENT is absence of agreement over candidates (Sokhey et al AJPS)
				*Note that it looks at all discussants, with reported opinions or not
gen disagreementweak3=1-numagree3/disctotal3
replace disagreementweak3=0 if disctotal3==0
replace disagreementweak3=. if p7_2>5

		*HETEROGENEITY 
replace agree31=0 if p50a_3>5 & p48a_3==1 & p7_2<6
replace agree32=0 if p50b_3>5 & p48b_3==1 & p7_2<6
replace agree33=0 if p50c_3>5 & p48c_3==1 & p7_2<6

recode agree31 0=1 1=0, gen(disagree31)
recode agree32 0=1 1=0, gen(disagree32)
recode agree33 0=1 1=0, gen(disagree33)

egen numdisagree3=rowtotal(disagree31 disagree32 disagree33)
forvalues i=1(1)3 {
replace noop3`i'=. if disagree3`i'==0  
}

egen noopsum_numerator3=rsum(noop31 noop32 noop33)
gen felipediscprop3weak=felipesum_numerator3 / numdisagree3
gen madrazodiscprop3weak= madrazosum_numerator3 /numdisagree3
gen amlodiscprop3weak=amlosum_numerator3 /numdisagree3
gen campadiscprop3weak= campasum_numerator3/numdisagree3
gen mercadodiscprop3weak=mercadosum_numerator3/numdisagree3
gen noopdiscprop3weak=noopsum_numerator3/numdisagree3
gen heterogeneityweak3 = 1-(felipediscprop3weak^2+madrazodiscprop3weak^2+amlodiscprop3weak^2+campadiscprop3weak^2+mercadodiscprop3weak^2+noopdiscprop3weak^2)
recode heterogeneityweak3 .=0
replace heterogeneityweak3 =. if numdisagree3<2 & numdisagree3~=. & disagreementweak3==. 
replace heterogeneityweak3=. if respond3==0
replace heterogeneityweak3=. if p7_2>6

		*SINGLETONS
recode disctotal3 0=1 .=. else=0, gen(zerodiscweak3)
replace zerodiscweak3=. if respond2==0

*************

	*GET QUICK CROSSTABS
preserve
replace disagreementstrong2=-1 if zerodiscstrong2==1
svy: tab disctotal2 if respond2==1 & p7_2<6
svy: tab disagreementstrong2 if respond2==1 & p7_2<6
svy: tab disagreementstrong2 heterogeneitystrong2 if respond2==1 & p7_2<6, cel
restore

	*IMPORTANT: HETEROGENEITY _within_ DISAGREEMENT
replace heterogeneitystrong2=0 if heterogeneitystrong2<.5 & disagreementstrong2<1
replace heterogeneitystrong3=0 if heterogeneitystrong3<.5 & disagreementstrong3<1

	*OTHER IVS
		*MEDIA
recode p15a p15b p16a_2 p16b_2 p13a_3 p13b_3 (1=4) (2=3) (3=2) (4=1) (else=.)

replace p15a=0 if p14a==1
replace p15b=0 if p14b==1
replace p15b=0 if p15b==. & p14b~=23

replace p16a_2=0 if p15a_2==1
replace p16b_2=0 if p15b_2==1
replace p16b_2=0 if p16b_2==. & p15b_2~=23
replace p16a_2=. if respond2==0
replace p16b_2=. if respond2==0

replace p13a_3=0 if p12a_3==1
replace p13b_3=0 if p12b_3==1
replace p13b_3=0 if p13b_3==. & p12b_3~=23
replace p13a_3=. if respond3==0
replace p13b_3=. if respond3==0

egen mediaattention1=rmean(p15a p15b)
egen mediaattention2=rmean(p16a_2 p16b_2)
egen mediaattention3=rmean(p13a_3 p13b_3)

egen mediaattention=rmean(mediaattention*)

		*MEDIA Networks
/*from TV content analysis2
Televisa								
	FC		31	6	68%
	AMLO	16	12	14%
Azteca	
	FC		40	12	54%
	AMLO	11	10	5%

Both		Pos	Neg	
	RMP	 	88 	31	48%


*/
label define tv 0 "none" 1 "televisa" 2 "azteca" 3 "other"

recode p13a_3 1=4 2=3 3=2 4=1, gen(tv_program1_freq)
recode p13b_3 1=4 2=3 3=2 4=1, gen(tv_program2_freq)
 
recode p12a_3 1=0 2=3 3=2 4=3 5=3 6=3 7=1 8=1 9/13=2 14=3 15=1 ///
16=1 17=1 18/19=3 20=1 21=2 22=3 23=0 24=3 25=3 26=1 27=1 28=3 29=3 30=3 31=3 32=3 .=., gen(tv_program1)
label values tv_program1 tv 
 
recode p12b_3 1=0 2=3 3=2 4=3 5=3 6=3 7=1 8=1 9/13=2 14=3 15=1 ///
16=1 17=1 18/19=3 20=1 21=2 22=3 23=0 24=3 25=3 26=1 27=1 28=3 29=3 30=3 31=3 32=3 .=., gen(tv_program2)
label values tv_program2 tv 

gen televisa_expose=0 if respond3==1
replace televisa_expose=tv_program1_freq if tv_program1==1 & tv_program2~=1
replace televisa_expose=tv_program2_freq if tv_program2==1 & tv_program1~=1
replace televisa_expose=tv_program1_freq + tv_program2_freq  if tv_program1==1 & tv_program2==1

gen azteca_expose=0 if respond3==1
replace azteca_expose=tv_program1_freq if tv_program1==2 & tv_program2~=2
replace azteca_expose=tv_program2_freq if tv_program2==2 & tv_program1~=2
replace azteca_expose=tv_program1_freq + tv_program2_freq  if tv_program1==2 & tv_program2==2

gen other_expose=0 if respond3==1
replace other_expose=tv_program1_freq if tv_program1==3 & tv_program2~=3
replace other_expose=tv_program2_freq if tv_program2==3 & tv_program1~=3
replace other_expose=tv_program1_freq + tv_program2_freq  if tv_program1==3 & tv_program2==3

gen televisaFC=.68-(.14+.48)/2
gen televisaAMLO=.14-(.68+.48)/2
gen televisaMad=.48-(.68+.14)/2
gen televisa = .
replace televisa=televisa_expose*(-televisaFC) if vote2==1
replace televisa=televisa_expose*(-televisaAMLO) if vote2==3
replace televisa=televisa_expose*(-televisaMad) if vote2==2

gen aztecaFC=.54-(.05+.48)/2
gen aztecaAMLO=.05-(.54+.48)/2
gen aztecaMad=.48-(.54+.05)/2
gen azteca = .
replace azteca=azteca_expose*(-aztecaFC) if vote2==1
replace azteca=azteca_expose*(-aztecaAMLO) if vote2==3
replace azteca=azteca_expose*(-aztecaMad) if vote2==2

egen disagreementmedia3=rsum(televisa azteca) if respond3==1
gen zeromedia3=0 if respond3==1
replace zeromedia3=1 if tv_program1_freq==0 & tv_program2_freq==0 & respond3==1
gen othermedia3=0 if respond3==1
replace othermedia3 = 1 if zeromedia==0 & disagreementmedia3==0 & respond3==1

		*AWARENESS
recode p32a 4=1 1=0 2=0 3=0 5=0 6=0 7=0 8=0 9=0, gen(correct1)
recode p32b 2=1 1=0 3=0 4=0 5=0 6=0 7=0 8=0 9=1, gen(correct2)
recode p32c 1=1 2=0 3=0 4=0 5=0 6=0 7=0 8=1 9=0, gen(correct3)
recode p32d 3=1 1=0 2=0 4=0 5=0 6=0 7=1 8=0 9=0, gen(correct4)
recode p71 1=1 2=0 3=0 4=0, gen(correct5)
recode p72a-p72c p65a_3 p65b_3 p65c_3 (2=0)
gen correct6=p72a
gen correct7=p72b
gen correct8=p72c
gen correct12=p65a_3
gen correct13=p65b_3
gen correct14=p65c_3
egen correct_poderes=anycount(correct6 correct7 correct8), values(1)
egen correct_poderes3=anycount(correct12 correct13 correct14), values(1)
recode p69a_2 1=1 2/6=0 .=., gen(correct9)
replace correct9=. if respond2~=1
recode p69b_2 2=1 1=0 3/6=0 .=., gen(correct10)
replace correct10=. if respond2~=1
recode p69c_2 3=1 1/2=0 4/6=0 .=., gen(correct11)
replace correct11=. if respond2~=1
egen ft_know=rownonmiss(p21a p21b p21c p21d p21e p21f p21g p21h p21i p21j p21k p21l p21m)

svy: irt grm correct1 correct2 correct3 correct4 correct5 correct_poderes correct_poderes3 correct9 correct10 correct11 ft_know, difficult
predict irt_know, latent ebmeans
egen awarez=std(irt_know)


/*
polychoricpca correct1-correct8, score(awaretemp) nscore(1)
impute awaretemp correct1-correct8 if tipomues~=., gen(aware)
summ aware
gen awarez=(aware-r(mean))/r(Var)^.5
gen awarezsq=awarez^2

svy: logit changedatall awarez 
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

svy: logit switchedatall awarez 
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

*/

eststo clear
*Figure 4.9B, Table A.11
eststo: svy: logit changedatall awarez i.p8 i.p7_2
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

eststo: svy: logit switchedatall awarez 
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans

	*timing
gen timing=1 if p8==p7_2 & p7_2==p6_3 & p8==p6_3 & p6_3~=. & p7_2~=.
replace timing=2 if p8~=p7_2 & p7_2==p6_3 & p6_3~=. & p7_2~=.
replace timing=3 if p7_2~=p6_3 & p6_3~=. & p7_2~=. 

replace timing=. if p8==4 
replace timing=. if p6_3>6

*Table A.11
svy: ologit timing awarez 
margin, at((min) awarez) at((max) awarez) contrast(atcontrast(r)) atmeans
margin, at((min) awarez) atmeans
margin, at((max) awarez) atmeans

*Figure 4.9A
margin, at((max) awarez) at((min) awarez) atmeans
marginsplot, xdimension(_outcome) yscale(range(0 .6)) ylab(0 "0%" .2 "20%" .4 "40%" .6 "60%" .8 "80%") xscale(range(.8 3.2)) ///
ytitle("{bf:Percentage of Voters}") legend(off) graphregion(color(white)) plotregion(lstyle(yxline) lcolor(black)) xlab(1 "Bef. campaign" 2 "Bef. late campaign" 3 "Bef. election day", labsize(small)) xtitle("{bf:Timing of Vote Decision}") ///
title("{bf:        Mexico 2006}", color(black) size(medsmall)) plot1opts(msymbol(0) lcolor(black) mcolor(black) ) plot2opts(msymbol(S) lcolor(gs7) mcolor(gs7) ) ci1opts(color(black)) ci2opts(color(gs7)) ///
text(.705 1.39 "Max knowledge", size(small)) text(.350 2.62 "Min knowledge", color(gs7) size(small)) text(0 1 "{it:N}=1,053", size(small)) 
graph save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Mex2006timing.gph", replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Mex2006timing.tif", as(tif) replace


cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 est2 using figure49d.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Vote Switching by Political Knowledge)

*eststo clear
label variable disagreementstrong2 "Network disagreement"
label variable heterogeneitystrong2 "Disagreeing alters’ diversity"
label variable zerodiscstrong2 "Singleton"

*eststo: svy: logit change23 disagreementstrong2 heterogeneitystrong2 zerodiscstrong2 
*Figure 4.3, Table A.3
svy: logit change23 disagreementstrong2 heterogeneitystrong2 zerodiscstrong2 
margin, at(disagreementstrong2=1 zerodiscstrong2==0 heterogeneitystrong2==0)
margin, at(disagreementstrong2=1 zerodiscstrong2==0 heterogeneitystrong2==.44444444)
margin, at(disagreementstrong2=.6666666 zerodiscstrong2==0 heterogeneitystrong2==0)
margin, at(disagreementstrong2=0 zerodiscstrong2==1 heterogeneitystrong2==0)
margin, at(disagreementstrong2=0 zerodiscstrong2==0 heterogeneitystrong2==0)

*esttab est1 using figure43d.rtf, label b(3) replace se star(* 0.05 ) nogap onecell title(Changed Vote Preference between Campaign and Election Waves by Campaign-Wave Network Traits: Results from Four Binary Logit Models )
	
svy: mean awarez if p6_3==1 & switchedatall==0
svy: mean awarez if p6_3==1 & switchedatall==1
 
svy: mean awarez if p6_3==2 & switchedatall==0
svy: mean awarez if p6_3==2 & switchedatall==1

svy: mean awarez if p6_3==3 & switchedatall==0
svy: mean awarez if p6_3==3 & switchedatall==1

/*		* SES
recode p59 10=., gen(eductemp)
gen educ=ln(eductemp)

recode p64a-p64h (2=0)
polychoricpca p64a p64b p64c p64d p64e p64f p64g p64h, score(wealthtemp1) nscore(1)
impute wealthtemp1 p64a p64b p64c p64d p64e p64f p64g p64h if tipomues~=., gen(wealth1)

recode p88 1=5 2=4 3=3 4=2 5=1 else=.
recode p89 1=5 2=4 3=3 4=2 5=1 else=.

polychoricpca wealth1 educ p88 p89, score(ses) nscore(1)
impute ses1 wealth1 educ p88 p89 if tipomues~=., gen(ses)
*/

		* AGE
gen age = ln(d)

		* FEMALE
recode a 1=0 2=1, gen(female)


*-----------------

*PARTY CLIENTELISM
recode p30a_3-p30g_3 (1/6=1) (else=.)
gen pan_client3=1 if p30a_3==1 | p30e_3==1
gen pri_client3=1 if p30b_3==1 | p30f_3==1
gen prd_client3=1 if p30c_3==1 | p30g_3==1
gen other_client3=1 if p30h_3==1 | p30d_3==1
recode pan_client3 pri_client3 prd_client3 other_client3 (.=0)
egen totclient3=rsum(pan_client3 pri_client3 prd_client3 other_client3)
replace totclient3=2 if p29_3==3
replace totclient3=. if respond3==0

gen panclientprop3=pan_client3 / totclient3
gen priclientprop3=pri_client3 / totclient3
gen prdclientprop3=prd_client3 / totclient3
gen otherclientprop3=other_client3 / totclient3

gen heterogeneityclient3 = 1-(panclientprop3^2+priclientprop3^2+prdclientprop3^2+otherclientprop3^2)
replace heterogeneityclient3=0 if totclient3==0
replace heterogeneityclient3=. if respond3==0
recode totclient3 0=1 .=. else=0, gen(zeroclient3)

		*Agreement and disagreement
gen agreeclient31=0
replace agreeclient31=1 if pan_client3==1 & p7_2==1
gen agreeclient32=0
replace agreeclient32=1 if prd_client3==1 & p7_2==3
gen agreeclient33=0
replace agreeclient33=1 if pri_client3==1 & p7_2==2
replace agreeclient31=. if respond3==0
replace agreeclient32=. if respond3==0
replace agreeclient33=. if respond3==0
egen numagreeclient3=rsum(agreeclient31-agreeclient33)
replace numagreeclient3=. if respond3==0
replace numagreeclient3=. if p7_2>6

gen disagreementclient3=1-numagreeclient3/totclient3
replace disagreementclient3=0 if totclient3==0
replace disagreementclient3=. if p7_2>6
replace disagreementclient3 = 1 if disagreementclient3>0 & disagreementclient3<1

*----------------------------

*PARTY CONTACTING 
gen pan_contact3=1 if p28a_3==1 | p28e_3==1
gen pri_contact3=1 if p28b_3==1 | p28f_3==1
gen prd_contact3=1 if p28c_3==1 | p28g_3==1
gen other_contact3=1 if p28d_3==1 | p28h_3==1
recode pan_contact3 pri_contact3 prd_contact3 other_contact3 (.=0)
egen totcontact3=rsum(pan_contact3 pri_contact3 prd_contact3 other_contact3)
replace totcontact3=. if p28_3==3
replace totcontact3=. if respond3==0

gen pancontactprop3=pan_contact3 / totcontact3
gen pricontactprop3=pri_contact3 / totcontact3
gen prdcontactprop3=prd_contact3 / totcontact3
gen othercontactprop3=other_contact3 / totcontact3

gen heterogeneitycontact3 = 1-(pancontactprop3^2+pricontactprop3^2+prdcontactprop3^2+othercontactprop3^2)
replace heterogeneitycontact3=0 if totcontact3==0
replace heterogeneitycontact3=. if respond3==0
recode totcontact3 0=1 .=. else=0, gen(zerocontact3)

		*Agreement and disagreement
gen agreecontact31=0
replace agreecontact31=1 if pan_contact3==1 & p7_2==1
gen agreecontact32=0
replace agreecontact32=1 if prd_contact3==1 & p7_2==3
gen agreecontact33=0
replace agreecontact33=1 if pri_contact3==1 & p7_2==2
replace agreecontact31=. if respond3==0
replace agreecontact32=. if respond3==0
replace agreecontact33=. if respond3==0
egen numagreecontact3=rsum(agreecontact31-agreecontact33)
replace numagreecontact3=. if respond3==0
replace numagreecontact3=. if p7_2>6

gen disagreementcontact3=1-numagreecontact3/totcontact3
replace disagreementcontact3=0 if totcontact3==0
replace disagreementcontact3=. if p7_2>6

*-------------------------------------------------------------------------

		*PARTISAN INTENSITY (IV)
recode p22 1=1 2=.5 3=1 4=.5 5=1 6=.5 7=0 8=0 9=0,  gen(partyintense1)

gen client_3=p29_3
replace client_3=. if client_3==3 
recode client_3 (1=1) (2=0)

xyz
		*THRESHOLD 
	*For Calderon voters
egen tempamlomadrazo=rowmean(p20b_2 p20c_2)
egen tempfc=rowmean(p20a_2)
gen fc_therm2=tempfc-tempamlomadrazo

	*For Madrazo voters
egen tempfcamlo=rowmean(p20a_2 p20c_2)
egen tempmad=rowmean(p20b_2)
gen madrazo_therm2=tempmad-tempfcamlo

	*For AMLO voters
egen tempfcmadrazo=rowmean(p20a_2 p20b_2)
egen tempamlo=rowmean(p20c_2)
gen amlo_therm2=tempamlo-tempfcmadrazo

	*For Mercado voters
egen tempnonminor=rowmean(p20a_2 p20b_2 p20c_2)
gen mercado_therm2=p20e_2-tempnonminor

	*For Campa voters
gen campa_therm2=p20d_2-tempnonminor

gen threshold2=fc_therm2 if p7_2==1
replace threshold2=madrazo_therm2 if p7_2==2
replace threshold2=amlo_therm2 if p7_2==3
replace threshold2=campa_therm2 if p7_2==4
replace threshold2=mercado_therm2 if p7_2==5
*replace threshold2=0 if threshold2<0
replace threshold2=(threshold2+10)/20


bysort change23: summ awarez partyintense1
bysort change23 zerodiscstrong2: summ disagreementstrong2 
bysort change23 zerodiscweak2: summ disagreementweak2 

*Labels
label variable heterogeneitystrong3 "Alters' diversity (strong)e wave"
label variable heterogeneitystrong2 "Alters' diversity (strong)c wave"
label variable disagreementstrong3 "Network disagreement (strong)e wave"
label variable disagreementstrong2 "Network disagreement (strong)c wave"
label variable zerodiscstrong3 "Isolates (strong)e wave"
label variable zerodiscstrong2 "Isolates (strong)c wave"
label variable awarez "Political awareness"
label variable partyintense1 "Threshold: strength of partisanship"
label variable threshold2 "Threshold: candidate evaluations"

label variable heterogeneityweak3 "Alters' diversity (weak)e wave"
label variable heterogeneityweak2 "Alters' diversity (weak)c wave"
label variable disagreementweak3 "Network disagreement (weak)e wave"
label variable disagreementweak2 "Network disagreement (weak)c wave"
label variable zerodiscweak3 "Isolates (weak)e wave"
label variable zerodiscweak2 "Isolates (weak)c wave"

label variable disctotal2 "Number of discussantsw2"
label variable disctotal2sq "Number of discussants2w2"

label variable disagreementcontact3 "Disagreement of party contactse wave"
label variable heterogeneitycontact3 "Diversity of party contactse wave" 
label variable zerocontact3 "Not contacted by a partye wave"
label variable disagreementclient3 "Benefit from a disagreeing partye wave"
label variable zeroclient3 "No benefit from a partye wave"

label variable change23 "DV: Switched vote intention"
svy: mean disagreementstrong2

*AGGREGATE 
preserve
	*Partisan intensity 
recode p22 1=1 2=.5 3=0 4=0 5=0 6=0 7=0 8=0 9=0, gen(panintense1)
svy: mean panintense1
mat bpan=e(b)
recode p22 1=0 2=0 3=1 4=.5 5=0 6=0 7=0 8=0 9=0, gen(priintense1)
svy: mean priintense1
mat bpri=e(b)
recode p22 1=0 2=0 3=0 4=0 5=1 6=.5 7=0 8=0 9=0, gen(prdintense1)
svy: mean prdintense1
mat bprd=e(b)
svmat bpan, names(panintensetemp)
svmat bpri, names(priintensetemp)
svmat bprd, names(prdintensetemp)
egen partyintensePAN=mean(panintensetemp)
egen partyintensePRI=mean(priintensetemp)
egen partyintensePRD=mean(prdintensetemp)

	*Stability
recode vote2 vote3 (7=.), gen(presvote2 presvote3) 
label values presvote2 presvote3 presvote

gen calderonstability=0 if presvote2==1 & presvote3~=.
replace calderonstability=1 if presvote3==1 & presvote2==1

gen madrazostability=0 if presvote2==2 & presvote3~=.
replace madrazostability=1 if presvote3==2 & presvote2==2

gen amlostability=0 if presvote2==3 & presvote3~=.
replace amlostability=1 if presvote3==3 & presvote2==3

*to get complement of their vote total in campaign wave
recode presvote2 1=0 .=. else=1, gen(calderonvotetemp1)
svy: mean calderonvotetemp1
mat bpan2=e(b)
recode presvote2 2=0 .=. else=1, gen(madrazovotetemp1)
svy: mean madrazovotetemp1
mat bpri2=e(b)
recode presvote2 3=0 .=. else=1, gen(amlovotetemp1)
svy: mean amlovotetemp1
mat bprd2=e(b)
svmat bpan2, names(calderonvotetemp1)
svmat bpri2, names(madrazovotetemp1)
svmat bprd2, names(amlovotetemp1)
egen calderonvote=mean(calderonvotetemp1)
egen madrazovote=mean(madrazovotetemp1)
egen amlovote=mean(amlovotetemp1)

replace madrazo_therm2=. if presvote2~=2
replace fc_therm2=. if presvote2~=1
replace amlo_therm2=. if presvote2~=3
replace calderonvote=. if presvote2~=1
replace madrazovote=. if presvote2~=2
replace amlovote=. if presvote2~=3
replace partyintensePAN=. if presvote2~=1
replace partyintensePRD=. if presvote2~=3
replace partyintensePRI=. if presvote2~=2

collapse disagreementstrong2 madrazo_therm2 fc_therm2 amlo_therm2 calderonvote madrazovote amlovote calderonstability madrazostability amlostability partyintensePAN partyintensePRD partyintensePRI [pweight=pweight], by(presvote2) 
keep if presvote2<4
egen threshold=rowmean(madrazo_therm2 fc_therm2 amlo_therm2)
egen votecomplement=rowmean(calderonvote madrazovote amlovote )
egen stability=rowmean(calderonstability madrazostability amlostability )
egen partyintense=rowmean(partyintensePAN partyintensePRD partyintensePRI )
gen year=2006
rename presvote2 presvote
rename disagreementstrong2 disagreementstrong
recode presvote 1=101 2=102 3=103
drop madrazo_therm2 - partyintensePRI 
save "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Data\agg06mex.dta", replace
restore


*REGRESSIONS
recode p7_2 4/6=6 7/9=., gen(presvote2)

	*Listwise deletion
eststo clear
eststo: svy: logit change23 disagreementstrong3 disagreementstrong2 heterogeneitystrong3 heterogeneitystrong2 zerodiscstrong3 zerodiscstrong2 ///
disagreementclient3 zeroclient3 disagreementmedia3 othermedia3 zeromedia3 i.presvote2

margins , at(disagreementstrong3==0 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans
margins , at(disagreementstrong3==1 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans
margins , at(disagreementstrong3==0 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=1 zerodiscstrong2=0) atmeans

margins , at(disagreementstrong3==.6666 disagreementstrong2==0 heterogeneitystrong3==0 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans
margins , at(disagreementstrong3==.6666 disagreementstrong2==0 heterogeneitystrong3==.5 heterogeneitystrong2==0 zerodiscstrong3=0 zerodiscstrong2=0) atmeans

*Figure 4.4D, Table A.4
coefplot (est1, mcolor(black) ciopts(lcolor(black) recast(rcap))), ///
drop(_cons othermedia3 1.presvote2 2.presvote2 3.presvote2 6.presvote2) xline(0, lcolor(black)) ///
coeflabels(disagreementstrong3="Network.disagreement{sub:{it:e}}" disagreementstrong2="Network.disagreement{sub:{it:c}}" heterogeneitystrong3="Disagreeing.alters'.diversity{sub:{it:e}}" ///
heterogeneitystrong2="Disagreeing.alters'.diversity{sub:{it:c}}" zerodiscstrong3="Singleton{sub:{it:e}}" zerodiscstrong2="Singleton{sub:{it:c}}" disagreementclient3="Paid off by a disagreeing party" ///
zeroclient3="Not paid off by a party" disagreementmedia3="Exposure to disagreeing TV news" zeromedia3="Unexposed to TV news", labsize(medsmall)) ///
legend(off) graphregion(color(white)) order() plotregion(lstyle(yxline) lcolor(black)) ///
msize(medsmall) groups(disagreementstrong3 disagreementstrong2 heterogeneitystrong3 heterogeneitystrong2 zerodiscstrong3 zerodiscstrong2 disagreementcontact3 disagreementclient3 zeroclient3 disagreementmedia3 zeromedia3="{bf:Independent Variables: Political Intermediaries}", labsize(medium)) ///
headings(disagreementstrong3 = "{bf:Horizontal}" disagreementclient3 = "{bf:Vertical}") ///
xscale(range(-3.5 3.5)) xlab(-5 -4 -3 -2 -1 0 1 2 3 4 5) ///
xtitle("{bf:Logit Coefficient}", size(medium)) yscale(titlegap(53)) 
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Figures\Mex2006IndyCoefPlot.tif", as(tif) replace
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_4D.tif", as(tif) replace width(1500)
graph export "C:\Users\Andy baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Publishers\Final Manuscript\Final Figures\BAR4_4D.pdf", as(pdf) replace 

*Table A.5
eststo: svy: logit change23 disagreementweak3 disagreementweak2 heterogeneityweak3 heterogeneityweak2 zerodiscweak3 zerodiscweak2 ///
disagreementclient3 zeroclient3 disagreementmedia3 othermedia3 zeromedia3 i.presvote2 
xyz
margins , at(disagreementweak3==0 disagreementweak2==0 heterogeneityweak3==0 heterogeneityweak2==0 zerodiscweak3=0 zerodiscweak2=0) atmeans
margins , at(disagreementweak3==1 disagreementweak2==0 heterogeneityweak3==0 heterogeneityweak2==0 zerodiscweak3=0 zerodiscweak2=0) atmeans

eststo: svy: logit change23 disagreementstrong3 disagreementstrong2 heterogeneitystrong3 heterogeneitystrong2 zerodiscstrong3 zerodiscstrong2 ///
disagreementcontact3 zerocontact3 i.presvote2 

eststo: svy: logit change23 disagreementweak3 disagreementweak2 heterogeneityweak3 heterogeneityweak2 zerodiscweak3 zerodiscweak2 ///
disagreementcontact3 zerocontact3 i.presvote2 

cd "C:\Users\Andy Baker\OneDrive - UCB-O365\My Documents\Research\Paper Book Networks\Chapter 4 Campaigns\Code\Output\"
esttab est1 est2 est3 est4 using Mexico2006indy.rtf, ///
label b(3) replace se star(* 0.05 ) nogap onecell title(Table: Switching in Mexico) ///
modelwidth(6) nonumbers mtitles("1" "2" "3" "4") compress

xyz
